package com.leyou.item.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpecService {
    @Autowired
    private SpecGroupMapper specGroupMapper;
    @Autowired
    private SpecParamMapper specParamMapper;

    public List<SpecGroup> queryGroupByCid(Long cid){
        QueryWrapper<SpecGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cid",cid);
        List<SpecGroup> specGroups = specGroupMapper.selectList(queryWrapper);
        return specGroups;
    }
    public void updateGroupByGid(SpecGroup specGroup){
        int i = specGroupMapper.updateById(specGroup.getId(), specGroup.getName());
    }
    public void insertGroup(SpecGroup specGroup){
        int i = specGroupMapper.insert(specGroup);
    }
    public void deleteGroupByGid(Long gid){
        //先删除组
        specGroupMapper.deleteById(gid);
        //再删除对应的参数
        specParamMapper.deleteByGId(gid);
    }
    public List<SpecParam> queryParams(Long gid,Long cid, Boolean generic,Boolean searching){
        QueryWrapper<SpecParam> queryWrapper = new QueryWrapper<>();
        if (gid != null){
            queryWrapper.eq("group_id",gid);
        }
        if (cid != null){
            queryWrapper.eq("cid",cid);
        }
        if (generic != null){
            queryWrapper.eq("generic",generic);
        }
        if (searching != null){
            queryWrapper.eq("searching",searching);
        }
        return specParamMapper.selectList(queryWrapper);
    }
    public void deleteParam(Long pid){
        specParamMapper.deleteById(pid);
    }
    public void insertParam(SpecParam specParam){
        //添加
        specParamMapper.insert(specParam);
    }
    public void updateParam(SpecParam specParam){
        specParamMapper.updateById(specParam);
    }
}
